package com.whl215.kit.grape.wrapper

import com.whl215.kit.adapter.GrapeViewHolder
import com.whl215.kit.adapter.wrap.GrapeNodeWrapper
import com.whl215.kit.grape.R
import com.whl215.kit.grape.model.GoodsBean
import com.whl215.kit.grape.model.ShopBean

/**
 * author： happiness
 * date： 2021/5/5
 * description：商店 分组节点 wrapper
 * @param shopType 商店类型 用来标记 商店包含商品的展示方式是 一行一个 还是一行两个
 *                  默认一行一个
 */
data class ShopNodeWrapper(private val item: ShopBean , val shopType:Int = ONE_IN_ROW) : ShopCartNodeWrapper() {
    companion object{
        const val ONE_IN_ROW = 1 // 一行一个
        const val TWO_IN_ROW = 2 // 一行两个
    }

    override fun getItemId(): String {
        return item.id
    }

    override fun onBindViewHolder(holder: GrapeViewHolder, position: Int) {
        holder.setChecked(R.id.item_cb_shop_selected,isSelected)
            .setText(R.id.item_tv_shop_name,item.name)
            .setText(R.id.item_btn_expend_collapse,if (isExpend) "收起" else "展开")
            .setOnClickListener(R.id.item_cb_shop_selected)
            .setOnClickListener(R.id.item_btn_expend_collapse)
            .setOnClickListener(R.id.item_btn_add)
    }

    override fun getViewType(): Int {
        return R.layout.item_wrapper_node_shop
    }

    override fun getItem(): Any {
        return item
    }


    override fun convertDataToWrapper(): MutableList<GrapeNodeWrapper> {
        val list = mutableListOf<GrapeNodeWrapper>()
        if (shopType == ONE_IN_ROW){
            for (goodsBean in item.children){
                list.add(GoodsOfOneInRowNodeWrapper(goodsBean))
            }
        }else if (shopType == TWO_IN_ROW){
            for (goodsBean in item.children){
                list.add(GoodsOfTwoInRowNodeWrapper(goodsBean))
            }
        }

        return list

    }

    override fun addChild(wrapper: GrapeNodeWrapper) {
        super.addChild(wrapper)
        item.children.add(wrapper.getItem() as GoodsBean)
    }

    override fun removeChild(wrapper: GrapeNodeWrapper) {
        super.removeChild(wrapper)
        item.children.remove(wrapper.getItem() as GoodsBean)
    }

    override fun getSpanSize(): Int {
        return 2
    }

    override fun getPrice(): Int {
        return 0
    }
}